<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\carwash;

use think\App;
use app\controller\property\Base;
use think\exception\ValidateException;
use app\validate\carwash\CarwashStoreValidate;
use app\common\repositories\carwash\CarwashLabelRepository;
use app\common\repositories\carwash\CarwashStoreRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;


class CarwashStore extends Base
{
    protected $repository;

    public function __construct(App $app, CarwashStoreRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyCarwashStoreAdd'),
            'editAuth' => property_auth('propertyCarwashStoreEdit'),
            'delAuth' => property_auth('propertyCarwashStoreDel'),
            'detailAuth' => property_auth('propertyCarwashStoreDetail'),
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('carwash/store/list');
    }

    public function add()
    {
        $propertyId = $this->request->param('property_id');
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'property_id' => '',
                'store_name' => '',
                'work_start' => '',
                'work_end' => '',
                'address' => '',
                'label_id' => '',
                'picture' => '',
                'longitude' => '',
                'latitude' => '',
                'is_show' => '',
                'remark' => '',
            ]);
            validate(CarwashStoreValidate::class)->scene('add')->check($param);
            try {
                $param['label_id'] = str_replace(['[', ']'], '', $param['label_id']);
                $param['label_id'] = array_filter(explode(',', $param['label_id']));
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加洗车站店失败', $e);
                return $this->error('网络失败');
            }
        } else {

            /** @var CarwashLabelRepository $carwashLabelRepository */
            $carwashLabelRepository = app()->make(CarwashLabelRepository::class);
            $labelData = $carwashLabelRepository->getLabelData($this->request->propertyId, ['is_show' => 1]);

            /** @var PropertyBasicRepository $propertyRepository */
            $propertyRepository = app()->make(PropertyBasicRepository::class);
            $propertyData = $propertyRepository->getPropertyData($this->request->propertyId, ['is_show' => 1]);
            return $this->fetch('carwash/store/add', [
                'labelData' => $labelData,
                'propertyId' => $propertyId,
                'propertyData' => $propertyData,
            ]);
        }

    }

    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'property_id' => '',
                'store_name' => '',
                'work_start' => '',
                'work_end' => '',
                'address' => '',
                'label_id' => '',
                'picture' => '',
                'longitude' => '',
                'latitude' => '',
                'is_show' => '',
                'remark' => '',
            ]);
            validate(CarwashStoreValidate::class)->scene('edit')->check($param);
            try {
                $param['label_id'] = str_replace(['[', ']'], '', $param['label_id']);
                $param['label_id'] = array_filter(explode(',', $param['label_id']));
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改洗车站店失败', $e);
                return $this->error('网络错误');
            }
        } else {
            /** @var CarwashLabelRepository $carwashLabelRepository */
            $carwashLabelRepository = app()->make(CarwashLabelRepository::class);
            $labelData = $carwashLabelRepository->getLabelData($this->request->propertyId, ['is_show' => 1]);

            /** @var PropertyBasicRepository $propertyRepository */
            $propertyRepository = app()->make(PropertyBasicRepository::class);
            $propertyData = $propertyRepository->getPropertyData($this->request->propertyId, ['is_show' => 1]);
            return $this->fetch('carwash/store/edit', [
                'info' => $info,
                'labelData' => $labelData,
                'propertyData' => $propertyData,
            ]);
        }
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除洗车站店失败', $e);
            return $this->error('网络失败');
        }
    }


    public function detail()
    {
        $carwashId = (int)$this->request->param('carwash_id');
        if ($carwashId <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($carwashId);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('carwash/store/detail', [
            'info' => $info
        ]);
    }

}